#ifdef MBM_IMIBASE_SUPPORT

#include "mbm_mtk_inc.h"

#include "mbm_typedefine.h"
#include "mbm_sys_adapter.h"
#include "mbm_vui_adapter.h"


#include "mbm_am_adapter.h"
#include "mbm_config.h"

//macro
#if defined(MBM_DEBUG_AM)
#define AM_TRACE mbm_trace
#else
#define AM_TRACE(...) 
#endif

//extern variable

//extern function

//static or gloable variable
static t_mbm_keyhandler g_am_key_handler = NULL;
static t_mbm_tphandler g_am_tp_handler = NULL;
static t_mbm_applet_state_handler g_am_state_handler = NULL;

//static function
static void mbm_am_key_handler(void)
{
    U16 code = 0, event = 0;
    UInt32 evt = 0, cd = 0;

	AM_TRACE("mbm_am_key_handler g_am_key_handler=%d", g_am_key_handler);
    GetkeyInfo(&code, &event);

    evt = mbm_translate_key_event(event);
    cd = mbm_translate_key_code(event, code);        

    if (g_am_key_handler)
    {
        (*g_am_key_handler)(evt, cd);
    }
}

void mbm_am_scr_reg_key_handler(void)
{
    ClearAllKeyHandler();

    SetGroupKeyHandler(
        mbm_am_key_handler, 
        (U16*)PresentAllKeys, 
        (U8)TOTAL_KEYS, 
        KEY_EVENT_UP);

    SetGroupKeyHandler(
        mbm_am_key_handler, 
        (U16*)PresentAllKeys, 
        (U8)TOTAL_KEYS, 
        KEY_EVENT_DOWN);
}

void mbm_am_reg_key_handler(t_mbm_keyhandler hdr)
{
	g_am_key_handler = hdr;
	if(g_am_key_handler)
	{
		mbm_am_scr_reg_key_handler();
	}
}

#if 0
void mbm_am_excute_key_handler(Int32 key_event, Int32 key_code)
{
	AM_TRACE("mbm_am_excute_key_handler");
	if(g_am_key_handler != NULL)
	{
		g_am_key_handler(key_event, key_code);
	}
}
#endif

static void mbm_pen_down_handler(mmi_pen_point_struct pos)
{
    if (g_am_tp_handler)
    {
        g_am_tp_handler(MBM_TP_EVENT_DOWN, pos.x, pos.y);    
    }
}

static void mbm_pen_up_handler(mmi_pen_point_struct pos)
{
    if (g_am_tp_handler)
    {
        g_am_tp_handler(MBM_TP_EVENT_UP, pos.x, pos.y);    
    }
}

static void mbm_pen_move_handler(mmi_pen_point_struct pos)
{
    if (g_am_tp_handler)
    {
        g_am_tp_handler(MBM_TP_EVENT_MOVE, pos.x, pos.y);    
    }
}

void mbm_am_scr_reg_tp_handler(void)
{
#if ( defined(__MMI_TOUCH_SCREEN__) || defined(__MMI_HANDWRITING_PAD__) ) && defined(__MTK_TARGET__)
	mmi_pen_register_down_handler(mbm_pen_down_handler);
	mmi_pen_register_up_handler(mbm_pen_up_handler);
	mmi_pen_register_move_handler(mbm_pen_move_handler);
#endif
}

void mbm_am_reg_tp_handler(t_mbm_tphandler hdr)
{
	g_am_tp_handler = hdr;
	if(g_am_tp_handler)
	{
		mbm_am_scr_reg_tp_handler();
	}
}

#if 0
void mbm_am_excute_tp_handler(Int32 tp_event, Int32 x, Int32 y)
{
	AM_TRACE("mbm_am_excute_tp_handler");
	if(g_am_tp_handler != NULL)
	{
		g_am_tp_handler(tp_event, x, y);
	}
}
#endif

void mbm_am_reg_state_handler(t_mbm_applet_state_handler hdr)
{
	g_am_state_handler = hdr;
}

void mbm_am_excute_state_handler(Int32 state)
{
	AM_TRACE("mbm_am_excute_state_handler");
	if(g_am_state_handler != NULL)
	{
		g_am_state_handler(state, 0);
	}
}

#endif /* MBM_IMIBASE_SUPPORT */

